const router = require('koa-router')()
// const query = require("../db/mysql");
const koa2Req = require('koa2-request');
var d = new Date()

router.get('/data',async (ctx,next)=>{
    ctx.status = 200
    let last_data 
    let day = d.getDate()-1
    let month = d.getMonth()+1
    let date = ''+d.getFullYear()+month+day
    let data = await koa2Req('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3b1a8ca642c70731&secret=fa46d039114f7b089c189d4bb6a578a5');
    let access_token = JSON.parse(data.body).access_token
    let body = JSON.stringify({
        begin_date:date,
        end_date:date
    })
    let res = await koa2Req({ 
        method: 'POST',
        url: `https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token=${access_token}`,
        body:body,
        headers: {
          'Content-Type': 'application/json'
        }
    })
    // console.log(res.body)
    return ctx.body = res.body
})

module.exports = router